<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!--地图-->
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
        }

        #allmap {
            height: 400px;
            width: 100%;
        }

        #r-result {
            width: 100%;
            font-size: 14px;
        }
    </style>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=WdNsD51pGIP96cBXIekofnPjhz6fkiur"></script>

    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理
            <small>体检机构地址管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="体检机构名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage1()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <!--数据展示表格-->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="机构名称" align="center"></el-table-column>
                <el-table-column prop="phoneNumber" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="longitudeAndLatitude" label="经纬度" align="center"></el-table-column>

                <el-table-column label="图片" align="center">
                    <template slot-scope="scope">
                        <img :src="scope.row.img" width="40" height="40" class="head_pic"/>
                    </template>
                </el-table-column>

                <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增体检机构地址信息" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="phoneNumber">
                                    <el-input v-model="formData.phoneNumber"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/setmeal/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input v-model="formData.longitudeAndLatitude" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="详细地址" prop="address" label-width="100px" style="position:relative">
                                    <el-input style="position: relative"
                                              id="myInput"
                                              v-model="formData.address"
                                              placeholder="请输入详细地址"
                                              suffix-icon="el-icon-search"
                                              disabled="disabled"
                                    >
                                        <i slot="suffix">
                                            <a href="javascript:;" id="myGprs" style="position: absolute">
                                                <img src="../img/location.jpg" style="height: 33px;width: 20px"
                                                     @click=" getGpsList">
                                            </a>
                                        </i>
                                    </el-input>

                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 点击详细地址定位按钮弹框 -->
            <el-dialog title="地图定位" :visible.sync="mapequipmentDialog" width="930px">
                <div class="selectAddress">
                    <div id="allmap"></div>
                    <div id="r-result" class="demo-input-suffix">
                        经度:
                        <el-input id="longitude" type="text" v-model="addobjequipment.longitude"
                                  style="width:100px; margin-right:10px;"></el-input>
                        纬度:
                        <el-input id="latitude" type="text" v-model="addobjequipment.latitude"
                                  style="width:100px; margin-right:10px;"></el-input>
                        <el-button icon="el-icon-search" circle @click="theLocation()"></el-button>
                    </div>
                    <p class="deviceAddressText" aria-placeholder="详细地址">{{dizhiMap}}</p>
                </div>
                <div id="container"></div>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="mapequipmentDialog = false">取 消</el-button>
                     <el-button type="primary" @click="getClick">确 定</el-button>
                 </span>
            </el-dialog>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑机构信息" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="机构名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="keyword">
                                    <el-input v-model="formData.phoneNumber"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/setmeal/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input v-model="formData.longitudeAndLatitude" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="详细地址" prop="address" label-width="100px" style="position:relative">
                                    <el-input style="position: relative"
                                              id="myInput1"
                                              v-model="formData.address"
                                              autocomplete="off"
                                              disabled="true"
                                              placeholder="请输入详细地址"
                                    >
                                        <i slot="suffix">
                                            <a href="javascript:;" id="myGprs1" style="position: absolute">
                                                <img src="../img/location.jpg" style="height: 40px;width: 20px"
                                                     @click="getGpsList">
                                            </a>
                                        </i>
                                    </el-input>

                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    /*校验方法*/
    const isNum = (rule, value, callback) => {
        const phoneNumber = /^\d{0,11}$/
        if (!phoneNumber.test(value)) {
            callback(new Error('电话只能为数字且个数不大于11位'))
        } else {
            callback();
        }
    };
    var map;
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 5,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },

            // 默认新增机构地址弹窗
            addequipmentDialog: false,
            // 存储地图的数据源
            addobjequipment: {
                address: '',//地址
                latitude: '',//经度
                longitude: '',//纬度
            },
            imageUrl: '',
            // 地图弹框
            mapequipmentDialog: false,
            // 地图弹框的地址数据
            dizhiMap: '',


            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                phoneNumber: [
                    {required: true, message: '电话为必填项', trigger: 'blur'},
                    {validator: isNum, message: '电话必须为纯数字', trigger: 'blur'}
                ],
                name: [{required: true, message: '机构名称为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //权限不足提示
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },

            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                this.$message({
                    type: response.flag ? "success" : "error",
                    message: response.message
                });
                if (response.flag) {
                    //预览刚刚上传的文件
                    this.imageUrl = "http://q7mxlxf7o.bkt.clouddn.com/" + response.data;
                    this.formData.img = response.data;//将文件名称赋值给模型数据，用于表单提交
                }
            },

            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },

            //编辑
            handleEdit() {
                //提交前编辑
                this.$refs['dataEditForm'].validate((valid) => {
                        if (valid) {
                            //校验通过
                            axios.post("/addressManagement/update.do", vue.formData).then((respons) => {
                                //回调函数
                                if (respons.data.flag) {
                                    //修改成功
                                    this.$message("修改成功"),
                                        type = "success",
                                        this.dialogFormVisible4Edit = false
                                    vue.findPage();
                                } else {
                                    //添加失败
                                    this.$message.error("修改失败")
                                }
                            })
                        } else {
                            //校验未通过
                            this.$message("校验未通过")
                            type = 'warning'
                        }
                    }
                ).catch((r) => {
                    this.showMessage(r)
                })
            },

            //将查询按钮独立出来
            findPage1() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //分页查询
            findPage() {
                axios.post("/addressManagement/findPage.do", this.pagination).then((response) => {
                    //为模型数据赋值，基于VUE的双向绑定展示到页面
                    this.dataList = response.data.data.rows;
                    this.pagination.total = response.data.data.total;
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = '';
                this.addobjequipment = {};
                this.dizhiMap = '';
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                axios.get("/addressManagement/findById.do?id=" + row.id).then((resp) => {
                    //赋值给表单
                    this.formData = resp.data.data;
                    this.dialogFormVisible4Edit = true;
                    this.imageUrl = "http://q7mxlxf7o.bkt.clouddn.com/" + resp.data.data.img;
                    //将表单数据赋值给地图
                    this.addobjequipment.longitude = this.formData.longitudeAndLatitude.split(',')[0];
                    this.addobjequipment.latitude = this.formData.longitudeAndLatitude.split(',')[1];
                    this.addobjequipment.address = this.formData.address;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //currentPage为切换后的新页码,将新页码赋值给parm;
                this.pagination.currentPage = currentPage;
                this.findPage()
            },
            // 删除
            handleDelete(row) {
                if (confirm("确认删除吗")) {
                    axios.get("/addressManagement/delById.do?id=" + row.id).then(function (resp) {
                        if (resp.data.flag) {
                            //删除成功
                            vue.findPage();
                        } else {
                            //删除失败
                            this.$message.error("删除失败")
                        }

                    }).catch((r) => {
                        this.showMessage(r)
                    })
                }
            },
            //提交表单
            handleAdd() {
                //提交前表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                        if (valid) {
                            //校验通过
                            axios.post("/addressManagement/add.do", vue.formData).then((respons) => {
                                //回调函数
                                if (respons.data.flag) {
                                    //添加成功
                                    this.$message("添加成功"),
                                        type = "success",
                                        vue.dialogFormVisible = false,
                                        vue.findPage();
                                } else {
                                    //添加失败
                                    this.$message.error("添加失败")
                                }
                            })
                        } else {
                            //校验未通过
                            this.$message("校验未通过")
                            type = 'warning'
                        }
                    }
                )
            },

            getGpsList() {
                /*初始化地图之前查看当前地图是否有数据*/
                /*该方法被新增和编辑按钮共用*/
                if (!this.addobjequipment.address) {
                    //如果地图没有数据
                    this.addobjequipment.longitude = 116.479889;
                    this.addobjequipment.latitude = 39.874257;
                    this.dizhiMap = "北京市朝阳区弘燕路";
                } else {
                    //地图有数据
                    this.dizhiMap = this.addobjequipment.address;
                }
                var that = this;
                this.mapequipmentDialog = true;
                this.$nextTick(function () {
                    // 创建变量，用于存储地址
                    var address;
                    var dizhi;
                    var marker;
                    if (marker === undefined) {
                        // 1.0 创建百度地图
                        map = new BMap.Map('allmap');
                        // 2.0 初始化地图坐标
                        var point = new BMap.Point(this.addobjequipment.longitude, this.addobjequipment.latitude);
                        // 3.0 设置地图坐标和地图级别
                        map.centerAndZoom(point, 10)
                        // var marker = new BMap.Marker(point);
                        // map.addOverlay(marker);
                        // 4.0 设置地图鼠标滚轮
                        map.enableScrollWheelZoom(true);
                        //创建标注
                        var mark = new BMap.Marker(point);
                        //添加标注
                        map.addOverlay(mark);
                        map.panTo(point);
                        // 5.0 地图添加点击事件
                        map.addEventListener('click', function (e) {
                            // 移除标注
                            map.removeOverlay(marker)
                            // 创建变量，用于存储经纬度
                            var point = e.point;
                            // console.log(point)

                            // 设置lng的值
                            window.localStorage.setItem('lng', point.lng)

                            // 设置lat的值
                            window.localStorage.setItem('lat', point.lat)

                            // 创建标注实例
                            marker = new BMap.Marker(point)

                            // 添加标注
                            map.addOverlay(marker)

                            // 创建地理编码实例
                            var geoc = new BMap.Geocoder()

                            geoc.getLocation(point, function (rs) {
                                //地图的点击事件
                                dizhi = rs.address;

                                address = rs.addressComponents;
                                // console.log(address)

                                // 设置currentProvince的值
                                window.localStorage.setItem('currentProvince', address.province)

                                // 设置currentCity的值
                                window.localStorage.setItem('currentCity', address.city)
                                // 设置地址的值
                                window.localStorage.setItem('dizhi', dizhi);
                                /*将坐标点信息返回到输入框*/
                                that.dizhiMap = window.localStorage.getItem('dizhi')
                                vue.addobjequipment.longitude = window.localStorage.getItem('lat');
                                vue.addobjequipment.latitude = window.localStorage.getItem('lng');
                            })
                        })
                    }
                })
            },
            // 用经纬度设置地图中心点
            theLocation() {
                if (document.getElementById("longitude").value != "" && document.getElementById("latitude").value != "") {
                    map.clearOverlays();
                    var new_point = new BMap.Point(document.getElementById("longitude").value, document.getElementById("latitude").value);
                    var marker = new BMap.Marker(new_point);  // 创建标注
                    map.addOverlay(marker);              // 将标注添加到地图中
                    map.panTo(new_point);
                }
            },
            /*提交地图弹窗数据*/
            getClick() {
                /*将地图数据赋值到addobjequipment对象内*/
                this.formData.address = this.dizhiMap;
                this.mapequipmentDialog = false;
                // console.log(that.addobjequipment.dizhiInput)
                this.addobjequipment.address = window.localStorage.getItem('dizhi');
                this.addobjequipment.latitude = window.localStorage.getItem('lat');
                this.addobjequipment.longitude = window.localStorage.getItem('lng');
                this.formData.longitudeAndLatitude = this.addobjequipment.longitude + "," + this.addobjequipment.latitude;
                this.editobjequipment.address = window.localStorage.getItem('dizhi');
                this.editobjequipment.latitude = window.localStorage.getItem('lat');
                this.editobjequipment.longitude = window.localStorage.getItem('lng');
            },
        }
    });
</script>
<script type="text/javascript">

</script>
</html>